﻿using System;
using System.Collections.Generic;
using Game.modules.properties;
using GameFramework.buff;
using GameFramework.data;
using UnityEngine;

namespace Game.modules.buff
{
    public class BuffPropertyCalScript:IPropertyCalScript
    {
        public int getId()
        {
            return FunctionIds.Buff;
        }

        public bool caculate(Dictionary<int,float> basicProperties,PlayerData playerData)
        {
            Debug.Log("buff计算属性！");
            //TODO 属性上限变化怎么算呢,一般只有血量和蓝量上限，其他上限属性有？
            bool change = false;
            Dictionary<int, float> newProperties = new Dictionary<int, float>();
            BuffManager buffManager = playerData.GetComponent<BuffManager>();
            List<BaseBuff> baseBuffs = buffManager.getAllBuffs();
            foreach (BaseBuff buff in baseBuffs)
            {
                BuffConfig buffConfig = buff.buffConfig;
                int multy = 1;
                if (buffConfig.overlapType == 1)//叠加层数
                {
                    multy = buff.layer;
                }

                if (buffConfig.type == 1)//固定属性
                {
                    int len = buffConfig.values.Length/2;
                    for (int i = 0; i < len; i++)
                    {
                        int key = (int)buffConfig.values[2 * i];
                        float value = buffConfig.values[2 * i+1];
                        if (!newProperties.ContainsKey(key))
                        {
                            newProperties.Add(key,value*multy);
                        }
                        else
                        {
                            newProperties[key] += value*multy;
                        }
                    }
                }
            }
            foreach (KeyValuePair<int,float> pair in newProperties)
            {
                if (basicProperties.ContainsKey(pair.Key))
                {
                    if (basicProperties[pair.Key] != pair.Value)
                    {
                        change = true;
                        basicProperties[pair.Key] = pair.Value;
                    }
                }
                else
                {
                    basicProperties.Add(pair.Key,pair.Value);
                    change = true;
                }
            }
            return change;
        }
        
        public bool caculatePercent(Dictionary<int,int> propertyPercent,PlayerData playerData)
        {
            return false;
        }
    }
}